<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Department extends PNC_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->model('mod_department');
	}

	/*
	 * Index
	 * @access public
	 * @return void
	 */

	public function index() {
		$data['menu'] = $this->mod_menu->show_menu();
		redirect('department/show_department/');
	}

	public function show_department() {
		if ($this->check_session()) {
			$data['title'] = 'Department';
			$config['total_rows'] = ''; // initialize total department
			$config['base_url'] = site_url("department/show_department/department/"); // initialize base url
			if ($this->uri->segment(3) === 'department' OR !$this->uri->segment(3)) {
				$config['base_url'] = site_url("department/show_department/department/");
				$config['total_rows'] = PNC_Model::count_data('sub_dept', 0, 'pnc_department');
			} else {
				$config['base_url'] = site_url("department/show_department/sub_department/");
				$config['total_rows'] = PNC_Model::count_data('sub_dept', 1, 'pnc_department');
			}
			$config['per_page'] = 5;
			$config['uri_segment'] = 4;
			$choice = $config["total_rows"] / $config["per_page"];
			$config["num_links"] = round($choice);
			$config['first_link'] = 'First';
			$config['next_link'] = 'Next';
			$config['prev_link'] = 'Pre';
			$config['last_link'] = 'Last';
			$this->pagination->initialize($config); //function to show all pages
			$page = ($this->uri->segment($config['uri_segment']) && $this->uri->segment($config['uri_segment']) > 0) ? $this->uri->segment($config['uri_segment']) : 0;
			if ($this->uri->segment(3) === 'department' OR !$this->uri->segment(3)) {
				$data['department'] = $this->mod_department->show_department($config['per_page'], $page);
			} else if ($this->uri->segment(3) === 'sub_department' OR !$this->uri->segment(3)) {
				$data['department'] = $this->mod_department->show_subdepartment($config['per_page'], $page);
			}
			$data['pagination'] = $this->pagination->create_links();
			$this->view_main($data);
		} else {
			redirect('main/');
		}
	}

	public function add_department() {
		$data['title'] = 'Add Department';
		if ($this->input->post('btn_add_dept')) {
			$dept_name = $this->input->post('txt_catename');
			//$this->mod_department->add_department($dept_name, 0);
			redirect('department/show_department');
		} else {
		//	$data['author'] = $this->mod_users->choose_users();
			$this->view_main($data);
		}
	}

	/*
	 * Delete Department
	 * @access public
	 */

	public function delete_department() {
		$page = $this->uri->segment(3);
		$id = $this->uri->segment(4);
		if (!$page || $page < 0)
			$page = 0;
		$this->mod_department->deleted($id);
		redirect('department/show_department');
	}

	public function department() {
		redirect('record/show_record/department/');
	}

	public function sup_department() {
		redirect("record/show_record/sup_department/");
	}

	public function view_main($data) {
		$this->load->view('master', $data);
	}

}